#coding=utf-8

import os
import sys
import json
import pickle
import time
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
import Conf


def main():
    card_list = os.listdir(Conf.ALL_USERS)
    for card in card_list:
        basic_info = pickle.load(open(os.path.join(Conf.ALL_USERS, card, card+"name_base")))
        struct_time = time.localtime()

        #循环账单列表,为每月的欠款计算利息,并写入到当月账单中
        for item in basic_info['debt']:
            interest = item['total_debt']*0.0005
            if basic_info[4]>= interest:
                basic_info[4] -= interest
            else:
                tmp = interest - basic_info[4]
                basic_info[4] = 0
                basic_info[0] -= temp
                pickle.dump(
                    basic_info,
                    open(os.path.join(Conf.ALL_USERS, card, card+'name_base'),'w')
                )
            data = time.shrftime("%Y-%m-%d")
            if struct_time.tm_day == 11 and basic_info[2]>0:
                dic = {'data':data,
                       'total_debt':basic_info[2],
                       'balance_debt':basic_info[2]
                       }
                basic_info[1].append(dic)
                basic_info[0] = 150000
            pickle.dump(
                basic_info,
                open(os.path.join(Conf.ALL_USERS,card, card+'name_base'),'w')
            )


if __name__ =='__main__':
    main()
